package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.Admin;
import com.geyue.core.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Lyon on 2017/6/2.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 跳转修改密码
     * @return
     */
    @RequestMapping(value = "/password", method = RequestMethod.GET)
    public String main(HttpSession session, Model model) {
        Admin admin = (Admin) session.getAttribute("admin");

        if(admin == null)
            return "error";

        return "admin/password";
    }

    @RequestMapping(value = "/password", method = RequestMethod.PUT)
    @ResponseBody
    public Res password(String password, String newPass, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");

        Admin admin1 = adminService.getOneByKey(admin.getId());

        if(admin1.getPassword().equals(password)){
            Admin param = new Admin();
            param.setId(admin.getId());
            param.setPassword(newPass);
            if(adminService.updateNotEmpty(param) > 0)
                return Res.success("修改成功");
        }

        return Res.fail("修改失败!");
    }
}
